package ace.module.account.core.impl.service.authentication.handler.impl;

import ace.cmp.core.exception.BusinessException;
import ace.cmp.id.generator.api.LongIdGenerator;
import ace.module.account.core.impl.constant.AccountCoreConstant;
import ace.module.account.core.impl.constant.AccountErrorCoreConstant;
import ace.module.account.core.impl.service.authentication.handler.RegisterQuickManager;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/3/16 9:33
 */
@Slf4j
@AllArgsConstructor
@Component
public class RegisterQuickManagerImpl implements RegisterQuickManager {
  private final LongIdGenerator idGenerator;

  /**
   * 支持快速注册的时候，进行回调
   *
   * @param hasAccount        是否有账户
   * @param registerQuickFlag 是否支持快速注册标志
   * @param consumer          value是sessionkey,支持快速注册的时候，进行回调
   */
  @Override
  public void ifSupportRegisterQuickThenAndThrow(
      boolean hasAccount, boolean registerQuickFlag, Consumer<String> consumer) {
    if (hasAccount) {
      return;
    }
    if (!registerQuickFlag) {
      return;
    }
    Map<String, Object> errorData = new HashMap<>();
    String sessionKey = idGenerator.get().toString();
    consumer.accept(sessionKey);
    errorData.put(AccountCoreConstant.REGISTER_QUICK_SESSION_KEY, sessionKey);
    throw new BusinessException(
        AccountErrorCoreConstant.SUPPORT_REGISTER_QUICK, "认证失败", errorData);
  }
}
